// pages/index/substituteCourse/substituteCourse.js
var util = require('../../utils/util');
let app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: [], //初始化获取的下拉框数据
        show_arr: [], //显示的下拉框数据
        index: 0, //初始化显示的数据
        children_id: '', //需要上传的学员的id
        sum: '', //控制class
        course: [], //选择课程的切换数据
        time_date: [], //选择课程的切换数据
        date: '', //日期选择获取数据
        data_time: '', //时间获取数据
        array1: '',
        data_item: '',
        wxSearchData:'',//搜索内容
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var This = this;
        var time = util.formatTime(new Date());
        This.setData({
            date: time.slice(0, 10)
        })
        this.loadFormate();
    },
    //load所有的接口
    loadFormate: function() {
        var This = this;
        
        //课程排期时间段选择接口
        app._get('coach/student/student_schedule', {
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            datetime: This.data.date
        }, function(res) {
            if (res.status != 0) {
                This.setData({
                    time_date: res.data, //时间显示数组
                })
            } else {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
        var name = '';
        if (This.data.wxSearchData != '') {
            name = This.data.wxSearchData
        } else {
            name = 0;
        }
        //选择学员接口
        app._get('coach/student/choice_student', {
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            name: name
        }, function(res) {
            if (res.code == 200) {
                if (res.list == '') {
                    wx.showToast({
                        title: '当前没有会员',
                        icon: 'loading',
                        showCancel: false,
                        duration: 1000,
                        mask: true
                    })
                    that.setData({
                        show_arr: []
                    })
                }else{
                    This.setData({
                        array: res.list, //学员显示数组
                        children_id: res.list[0].id,
                    })
                    var name = This.data.array;
                    var arrList = [];
                    for (var index in name) {
                        arrList.push(name[index].name + " " + name[index].phone)
                    }
                    // console.log(arrList)
                    This.setData({
                        show_arr: arrList
                    })
                    console.log(This.data.show_arr)
                    This.load_last();
                }
            } else {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    wxSearchInput:function(e){
        var that = this; 
        var name = '';
        if (e.detail.value != '') {
            name = e.detail.value;
        } else {
            name = 0;
        } 
        app._get('coach/student/choice_student', {
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            name: name
        }, function (res) {
            if (res.code == 200) {
                if (res.list == '') {
                    wx.showToast({
                        title: '当前没有会员',
                        icon: 'loading',
                        showCancel: false,
                        duration: 1000,
                        mask: true
                    })
                    that.setData({
                        show_arr:[]
                    })
                }else{
                    that.setData({
                        array: res.list, //学员显示数组
                        children_id: res.list[0].id,
                    })
                    var name = that.data.array;
                    var arrList = [];
                    for (var index in name) {
                        arrList.push(name[index].name + " " + name[index].phone)
                    }
                    // console.log(arrList)
                    that.setData({
                        show_arr: arrList
                    })
                    that.load_last();
                }
                
            } else {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    load_last: function() {
        var This = this;
        //课程类型接口
        app._get('coach/student/course_type', {
            member_id: This.data.children_id,
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
        }, function(res) {
            if (res.code == 200) {
                This.setData({
                    course: res.itemType,
                })

            } else {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            }
        })

    },
    //选择学员的select数据
    bindPickerChange: function(e) {
        var This = this;
        This.setData({
            index: e.detail.value,
        })
        for (var index in This.data.array) {
            if (index == e.detail.value) {
                This.setData({
                    children_id: This.data.array[index].id
                })
            }
        }
        //课程类型
        app._get('coach/student/course_type', {
            member_id: This.data.children_id,
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
        }, function(res) {
            if (res.code == 200) {
                console.log(res)
                This.setData({
                    course: res.itemType,
                })
            } else {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    //课程切换按钮
    active_cl: function(e) {
        var This = this
        This.setData({
            sum: e.target.id,
        })
    },
    //选择日期
    bindDateChange: function(e) {
        var This = this;
        console.log('picker发送选择改变，携带值为', e.detail.value)
        This.setData({
            date: e.detail.value
        })
        //课程排期时间段选择接口(选择日期后的刷新)
        app._get('coach/student/student_schedule', {
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            datetime: This.data.date
        }, function(res) {
            if (res.code == 200) {
                This.setData({
                    time_date: res.data, //时间显示数组
                })
            } else {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    //选择时间
    active_time: function(e) {
        var This = this;
        console.log(e)
        This.setData({
            data_time: e.currentTarget.dataset.value,
            data_item: e.target.id
        })
    },
    //提交数据
    add_to: function() {
        var This = this;
        //代约课程提交
        if (This.data.sum == '') {
            wx.showToast({
                title: '请选择课程',
                icon: 'loading',
                showCancel: false,
                duration: 3000,
                mask: true
            })
            return 0;
        }
        if (This.data.data_time == '') {
            wx.showToast({
                title: '请选择时间',
                icon: 'loading',
                showCancel: false,
                duration: 3000,
                mask: true
            })
            return 0;
        }
        app._get('coach/student/add_course', {
            member_id: This.data.children_id, //学员id
            item_id: This.data.sum, //课程id
            time: This.data.date, //日期选择
            start_time: This.data.data_time, //选择时间
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
        }, function(res) {
            if (res.status == 1) {
                wx.showToast({
                    title: '代约成功',
                    icon: 'none',
                    duration: 3000
                })
                setTimeout(function() {
                    wx.switchTab({
                        url: '../index/index',
                    })
                }, 1000)
            } else {
                wx.showToast({
                    title: res.error,
                    icon: 'loading',
                    icon: 'none',
                    duration: 3000,
                    mask: true
                })
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})